﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entity;
using BUS;

namespace GUI
{
    public partial class NhomTinForm : Form
    {
        public NhomTinForm()
        {
            InitializeComponent();
        }

        private void loadComboboxData()
        {

            BindingList<NhomTin> list = new NhomTinBUS().LoadAll();
            //hien thi len combobox
            cmbList.DataSource = list;
            cmbList.DisplayMember = "TenNhomTin";
            cmbList.ValueMember = "MaNhomTin";
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {

            loadComboboxData();
            btnDel.Enabled = true;
            btnUpdate.Enabled = true;
        }

        private void cmbList_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindingList<NhomTin> list = cmbList.DataSource as BindingList<NhomTin>;
            int index = cmbList.SelectedIndex;
            if ((0 <= index) && (index < list.Count))
            {
                NhomTin sv = list[index];
                txtMa.Text = sv.MaNhomTin.ToString();
                txtTen.Text = sv.TenNhomTin;
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            int id = int.Parse(txtMa.Text);
            int result = new NhomTinBUS().DeleteByStringId(id);
            //load lai data
            loadComboboxData();
            if (result <= 0)
                MessageBox.Show("Khong the xoa ma so: " + id);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (chkUpdate.Checked)
                UpdateSv();
            else
                InsertSv();
            loadComboboxData();
        }

        private void chkUpdate_CheckedChanged(object sender, EventArgs e)
        {
            if (!chkUpdate.Checked)
                btnUpdate.Text = "Thêm";
            else
                btnUpdate.Text = "Cập nhật";
        }
        private void UpdateSv()
        {
            // lay ma so sinh vien can cap nhat
            NhomTin sv = CreateSvObj();
            int result = new NhomTinBUS().Update(sv);
            if (result <= 0)
                MessageBox.Show("Khong the cap nhat.");
        }

        private NhomTin CreateSvObj()
        {

            int ms = 0;
            try
            {
                ms = int.Parse(txtMa.Text);
            }
            catch { 
            
            }
            string ht = txtTen.Text;
            NhomTin sv = new NhomTin();
            sv.MaNhomTin = ms;
            sv.TenNhomTin = ht;

            return sv;
        }

        private void InsertSv()
        {
            NhomTin sv = CreateSvObj();
            int result = new NhomTinBUS().Insert(sv);
            if (result <= 0)
                MessageBox.Show("Khong the them.");
        }
    }
}
